/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Microsoft
 * Date: 2023-03-05
 * Time: 10:02
 */
public class MyQueue {
    static class Node{
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }
    public Node head;
    public Node last;
    public int usedSize;

    //入队
    public void offer(int val){
        Node node = new Node(val);
        if (head == null) {
            head = node;
            last = node;
        }else {
            last.next = node;
            last = last.next;
        }
        usedSize++;

    }
    //出队
    public int poll(){
        if (empty()) {
            throw new EmptyException("队列为空");
        }
        int ret = head.val;
        head = head.next;
        if (head == null) {
            last = null; //如果只有一个节点，那么last也要置空
        }
        usedSize--;
        return ret;

    }
    //检查是否为空
    public boolean empty(){
        return usedSize == 0;
    }
    //
    public int peek(){
        if (empty()) {
            throw new EmptyException("队列为空");
        }
        return head.val;
    }
    public int getUsedSize(){
        return usedSize;
    }
}


